/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientserver_test_tim;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.WindowConstants;

/**
 *
 * @author timbeer
 */
public class Client implements Runnable {

    private Socket client;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    final static String HOST = "localhost";
    final static int PORT = 7777;
    private Person p;
ClientGUI gui;
    public Client(String HOST, int PORT) {

       
        
        
        //connect to server
        try {
            client = new Socket(HOST, PORT);

            System.out.println("Verbindung zu Port " + client);

            ois = new ObjectInputStream(client.getInputStream());
            oos = new ObjectOutputStream(client.getOutputStream());

            new Thread(this).start();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
 gui = new ClientGUI(this);
        gui.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        gui.setSize(400, 400);
        gui.setVisible(true);
    }

    //send the Object
    public void sendeObjekt(Person p) {
        try {
            oos.writeObject(p);

        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    //receive Objects forever
    public void run() {

        try {
            while (true) {
                try {
                    Person x = (Person) ois.readObject();
                    gui.updateFields(x);
                    
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } catch (IOException ie) {
            System.out.println(ie);
        }
    }
}
